package io.framework.modules.sys.controller;

import io.framework.common.annotation.LogOperation;
import io.framework.common.constant.Constant;
import io.framework.common.page.PageData;
import io.framework.common.utils.ExcelUtils;
import io.framework.common.utils.Result;
import io.framework.common.validator.AssertUtils;
import io.framework.common.validator.ValidatorUtils;
import io.framework.common.validator.group.AddGroup;
import io.framework.common.validator.group.DefaultGroup;
import io.framework.common.validator.group.UpdateGroup;
import io.framework.modules.sys.dto.SysAddressListDTO;
import io.framework.modules.sys.excel.SysAddressListExcel;
import io.framework.modules.sys.service.SysAddressListService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
 * 通讯录
 *
 * @author Gujq email
 * @since 1.0.0 2020-04-14
 */
@RestController
@Api(tags="通讯录")
public class SysAddressListController {
    @Autowired
    private SysAddressListService sysAddressListService;

    @GetMapping("sys/sysaddresslist/page")
    @ApiOperation("分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String"),
        @ApiImplicitParam(name = "username", value = "姓名", paramType = "query", dataType="String")
    })
    @RequiresPermissions("sys:sysaddresslist:list")
    public Result<PageData<SysAddressListDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        PageData<SysAddressListDTO> page = sysAddressListService.page(params);

        return new Result<PageData<SysAddressListDTO>>().ok(page);
    }

    @GetMapping("sys/sysaddresslist/{id}")
    @ApiOperation("信息")
    public Result<SysAddressListDTO> get(@PathVariable("id") Long id){
        SysAddressListDTO data = sysAddressListService.get(id);

        return new Result<SysAddressListDTO>().ok(data);
    }

    @PostMapping("sys/sysaddresslist")
    @ApiOperation("保存")
    @LogOperation("添加通讯录信息")
    @RequiresPermissions("sys:sysaddresslist:save")
    public Result save(SysAddressListDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        sysAddressListService.save(dto);

        return new Result();
    }

    @PutMapping("sys/sysaddresslist")
    @ApiOperation("修改")
    @LogOperation("修改通讯录信息")
    @RequiresPermissions("sys:sysaddresslist:update")
    public Result update(SysAddressListDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        sysAddressListService.update(dto);

        return new Result();
    }

    @DeleteMapping("sys/sysaddresslist/{id}")
    @ApiOperation("删除")
    @LogOperation("删除通讯录信息")
    @RequiresPermissions("sys:sysaddresslist:delete")
    public Result delete(@PathVariable("id") Long id){

        sysAddressListService.deleteById(id);

        return new Result();
    }

    @DeleteMapping("sys/sysaddresslist")
    @ApiOperation("批量删除")
    @LogOperation("批量删除通讯录信息")
    @RequiresPermissions("sys:sysaddresslist:deletes")
    public Result deletes(Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        sysAddressListService.delete(ids);

        return new Result();
    }

    @GetMapping("sys/sysaddresslist/export")
    @ApiOperation("导出")
    @LogOperation("导出")
    //@RequiresPermissions("sys:sysaddresslist:export")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<SysAddressListDTO> list = sysAddressListService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, list, SysAddressListExcel.class);
    }

}